--宝藏传说
--#include "data\config\activityconfig\DailyActivity\BaoZangConfig.lua" once 
--#include "data\functions\Activity\ActivityGuide.lua" once

function EnterBaoZangScene( sysarg )
	if not IsBaoZangActivityStart() then
		return
	end
	local rand = System.getRandomNumber(#BaoZangConfig.EnterPos) + 1	
	Actor.enterScene(sysarg,unpack(BaoZangConfig.EnterPos[rand]))

	OpenBaoZangPanel(sysarg)
end

--活动开始
function  BaoZangActivityBegin()
	local var = System.getDyanmicVar()
	var.baoZangActiivtyStart = System.getCurrMiniTime()
	var.baoZangActiivtyIdx = 1
	var.baoZangBossState = 0
	CreateBaoZangActivityMonster(var.baoZangActiivtyIdx)

	System.broadcastTipmsg(Lang.ScriptTips.BaoZang004, ttScreenCenter + ttChatWindow,BaoZangConfig.level)
end

function BaoZangActivityEnd()
	if not IsBaoZangActivityStart() then
		return
	end
	BaoZangKillAllPlayer()
	local var = System.getDyanmicVar()
	var.baoZangActiivtyStart = nil
	var.baoZangActiivtyIdx = nil
	var.baoZangBossState = nil
	System.broadcastTipmsg(Lang.ScriptTips.BaoZang005, ttScreenCenter + ttChatWindow,BaoZangConfig.level)
end

function IsBaoZangActivityStart()
	local var = System.getDyanmicVar()
	if var.baoZangActiivtyStart and var.baoZangActiivtyStart > 0 then
		return true
	end
	return false
end

function CreateBaoZangActivityMonster(ii)
	if not IsBaoZangActivityStart() then
		return
	end
	if not BaoZangConfig.Monsters then
		return
	end
	if not BaoZangConfig.Monsters[ii] then
		return 
	end
	for idx = 1, #BaoZangConfig.Monsters[ii] do
		BaoZangActivityCreateMon(ii, idx)
	end

	if BaoZangConfig.BroadCast[ii] and BaoZangConfig.BroadCast[ii] ~= "" then
		local msgStr = BaoZangConfig.BroadCast[ii]
		local scendId = BaoZangConfig.Monsters[1][1].scendId
		local hScene = Fuben.getSceneHandleById( scendId, 0) 
		if ii == #BaoZangConfig.BroadCast then
			local bossName = MonsterNameDeleteNumber(BaoZangConfig.bossId) or ""
			local pMonster = Fuben.getMonsterPtrById(hScene, BaoZangConfig.bossId)
			local posX, posY = 0, 0
			if pMonster then
				posX, posY = Actor.getEntityPosition(pMonster, 0, 0)
			end
			msgStr = string.format(msgStr, bossName, posX, posY)
		end
		Fuben.sendMsgToScene(hScene, msgStr, ttScreenCenter + ttChatWindow)
	end
	

	BaoZangChangeRightPanelAll()
end

function GetBaoZangTotalMon(ii)
	local count = 0
	if BaoZangConfig.Monsters and BaoZangConfig.Monsters[ii] then
		for idx = 1, #BaoZangConfig.Monsters[ii] do
			count = count + BaoZangConfig.Monsters[ii][idx].count
		end
	end
	return count
end

function BaoZangMonDie(monster, monId)
	--print("monId = "..monId)
	if not IsBaoZangActivityStart() then
		return
	end
	local var = System.getDyanmicVar()
	if monId == BaoZangConfig.bossId then
		var.baoZangBossState = 2
	end
	local sceneHandle = Actor.getSceneHandle(monster)
	local monCount = Fuben.getLiveMonsterCount(sceneHandle)
	if monCount == 0 then
		if var.baoZangActiivtyIdx >= BaoZangConfig.count then
			BaoZangChangeRightPanelAll()
			--BaoZangActivityEnd()
		else
			var.baoZangActiivtyIdx = var.baoZangActiivtyIdx + 1
			CreateBaoZangActivityMonster(var.baoZangActiivtyIdx)
		end
	else
		BaoZangChangeRightPanelAll()
	end
end

function BaoZangKillAllPlayer()
	local nSceneId = BaoZangConfig.EnterPos[1][1]
	local playerList = LuaHelp.getSceneActorListById(nSceneId)  --获取在线玩家对象列表	
	if(playerList == nil)then
		return
	end
	for k,v in pairs(playerList)do	--踢出场景
		Actor.enterScene(v,unpack(BaoZangConfig.ExitPos))	
		CloseActivityRightPanel(v)			--关闭活动面板
	end
end

--刷怪
function  BaoZangActivityCreateMon(ii, idx)
	if not IsBaoZangActivityStart() then
		return
	end
    local  cfg = BaoZangConfig
    local monstersCfg = cfg.Monsters[ii]
    if not monstersCfg then
    	return
    end
	local monCfg = monstersCfg[idx]
	if  not monCfg then
	    return 
	end
	local  dVar = System.getDyanmicVar()
	local  hScene = Fuben.getSceneHandleById(monCfg.scendId, 0)
	if hScene then
		Fuben.createMonstersInRange(hScene,monCfg.monId,monCfg.x1,monCfg.y1,monCfg.x2,monCfg.y2,monCfg.count,monCfg.livetime)
		local pMonster = Fuben.getMonsterPtrById(hScene, BaoZangConfig.bossId)
		if pMonster then
			local var = System.getDyanmicVar()
			var.baoZangBossState = 1
		end
	end  
end

function IsInBaoZangActivityScene(sysarg)
	if BaoZangConfig.EnterPos[1][1] == Actor.getSceneId(sysarg) then	
		return true
	end
	return false
end

--左侧面板初始化
function OpenBaoZangPanel( sysarg )
	if not IsInBaoZangActivityScene( sysarg ) then
		return
	end
	local var = System.getDyanmicVar()
	
	local activityType 	= 2 			--活动
	local activityId 	= enRightPanelActivity_BaoZang
	local restTime 		= var.baoZangActiivtyStart + BaoZangConfig.time - System.getCurrMiniTime()
	if restTime < 0 then
		restTime = 0
	end
	local panelInfo 	= {}
	panelInfo.sts 		= 1  									--进行中
	local sceneHandle = Actor.getSceneHandle(sysarg)
	local monCount = Fuben.getLiveMonsterCount(sceneHandle)
	if monCount == 0 then
		panelInfo.sts 		= 2
	end
	panelInfo.title 	= Lang.ScriptTips.BaoZangPanel001
	panelInfo.timeTitle = Lang.ScriptTips.BaoZangPanel002			--剩余时间
	panelInfo.restTime	= restTime

	panelInfo.buttons   	= {}
	panelInfo.buttons[1]	= 1							--1：退出副本，2-单倍领奖，3-双倍领奖, 4-查看排行榜
	

	panelInfo.contentTitle = ""
	panelInfo.contents = {}
	local sceneHandle = Actor.getSceneHandle(sysarg)
	local monCount = Fuben.getLiveMonsterCount(sceneHandle)
	local totalCount = GetBaoZangTotalMon(var.baoZangActiivtyIdx or 1)
	local killCount = totalCount - monCount
	if killCount < 0 then
		killCount = 0
	end
	panelInfo.contents[1] = string.format(Lang.ScriptTips.BaoZangPanel003, var.baoZangActiivtyIdx or 1)
	panelInfo.contents[2] = string.format(Lang.ScriptTips.BaoZangPanel004, killCount, totalCount)
	local bossName = MonsterNameDeleteNumber(BaoZangConfig.bossId) or ""
	if var.baoZangBossState and var.baoZangBossState == 1 then
		panelInfo.contents[3] = string.format(Lang.ScriptTips.BaoZangPanel005, bossName, Lang.ScriptTips.BaoZangPanel007)
	elseif var.baoZangBossState and var.baoZangBossState == 2 then
		panelInfo.contents[3] = string.format(Lang.ScriptTips.BaoZangPanel005, bossName, Lang.ScriptTips.BaoZangPanel008)
	else
		panelInfo.contents[3] = string.format(Lang.ScriptTips.BaoZangPanel005, bossName, Lang.ScriptTips.BaoZangPanel006)
	end
	--print("activityId ="..activityId)
	--print("activityType ="..activityType)
	OpenActivityRightPanel( sysarg, activityType, activityId, panelInfo )
end

--左侧面板改变
function ChangeBaoZangPanel( sysarg )
	if not IsInBaoZangActivityScene( sysarg ) then
		--print("ChangeCrossSixthPersonalityPanel not IsInCrossSixthPersonalityFuben")
		return
	end
	local panelInfo 	= {}
	panelInfo.sts 		= 1  									--进行中
	local sceneHandle = Actor.getSceneHandle(sysarg)
	local monCount = Fuben.getLiveMonsterCount(sceneHandle)
	if monCount == 0 then
		panelInfo.sts 		= 2
	end

	panelInfo.buttons   	= {}
	panelInfo.buttons[1]	= 1							--1：退出副本，2-单倍领奖，3-双倍领奖, 4-查看排行榜

	panelInfo.contentTitle = ""
	panelInfo.contents = {}
	local var = System.getDyanmicVar()
	local sceneHandle = Actor.getSceneHandle(sysarg)
	local monCount = Fuben.getLiveMonsterCount(sceneHandle)
	local totalCount = GetBaoZangTotalMon(var.baoZangActiivtyIdx or 1)
	local killCount = totalCount - monCount
	if killCount < 0 then
		killCount = 0
	end
	panelInfo.contents[1] = string.format(Lang.ScriptTips.BaoZangPanel003, var.baoZangActiivtyIdx or 1)
	panelInfo.contents[2] = string.format(Lang.ScriptTips.BaoZangPanel004, killCount, totalCount)
	local bossName = MonsterNameDeleteNumber(BaoZangConfig.bossId) or ""
	if var.baoZangBossState and var.baoZangBossState == 1 then
		panelInfo.contents[3] = string.format(Lang.ScriptTips.BaoZangPanel005, bossName, Lang.ScriptTips.BaoZangPanel007)
	elseif var.baoZangBossState and var.baoZangBossState == 2 then
		panelInfo.contents[3] = string.format(Lang.ScriptTips.BaoZangPanel005, bossName, Lang.ScriptTips.BaoZangPanel008)
	else
		panelInfo.contents[3] = string.format(Lang.ScriptTips.BaoZangPanel005, bossName, Lang.ScriptTips.BaoZangPanel006)
	end

	ChangeActivityRightPanel( sysarg, panelInfo )
end

function BaoZangOpenRightPanelAll()
	local nSceneId = BaoZangConfig.EnterPos[1][1]
	local playerList = LuaHelp.getSceneActorListById(nSceneId)  --获取在线玩家对象列表	
	if(playerList == nil)then
		return
	end
    for i, player in ipairs(playerList) do
        OpenBaoZangActivityPanel(player)
    end
end

function BaoZangChangeRightPanelAll()
	local nSceneId = BaoZangConfig.EnterPos[1][1]
	local playerList = LuaHelp.getSceneActorListById(nSceneId)  --获取在线玩家对象列表	
	if(playerList == nil)then
		return
	end
    for i, player in ipairs(playerList) do
        ChangeBaoZangPanel(player)
    end
end


--离开场景之后的处理
function BaoZangLeaveActivityHandler(sysarg)
	CloseActivityRightPanel(sysarg)	
end

--下线处理
function BaoZangLogoutHandler(sysarg)
	BaoZangLeaveActivity(sysarg)
end

--离开活动场景
function BaoZangLeaveActivity(sysarg)
	Actor.enterScene(sysarg,unpack(BaoZangConfig.ExitPos))	
end






